﻿using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_NewsCategory_Default : System.Web.UI.Page
{
    private NewsCategoryRepository repository = null;

    private List<NewsCategory> allCategories = new List<NewsCategory>();

    private TreeNode ConvertCategoryToNode(NewsCategory category)
    {
        TreeNode node = new TreeNode
        {
            Text = category.Name,
            Expanded = true,
            NodeID = category.ID.ToString()
        };
        node.CustomAttributes.Add(new ConfigItem("Name", category.Name, ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("Url", category.Link, ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("Order", Util.BoolableToString(category.IsOnHomePage), ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("IsActive", Util.BoolableToString(category.IsActive), ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("IsOnHomePage", Util.BoolableToString(category.IsOnHomePage), ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("IsTop", Util.BoolableToString(category.IsTop), ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("IsBottom", Util.BoolableToString(category.IsBottom), ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("IsLeft", Util.BoolableToString(category.IsLeft), ParameterMode.Value));
        node.CustomAttributes.Add(new ConfigItem("IsRight", Util.BoolableToString(category.IsRight), ParameterMode.Value));
        string order = "0";
        if (category.Order.HasValue)
        {
            order = category.Order.Value.ToString();
        }
        node.CustomAttributes.Add(new ConfigItem("Order", order, ParameterMode.Value));
        node.IsTarget = true;
        node.Draggable = true;
        node.AllowDrag = true;
        return node;
    }

    public TreeNodeCollection CreateChildNode(NewsCategory category)
    {
        TreeNodeCollection nodes = new TreeNodeCollection();
        TreeNode node = this.ConvertCategoryToNode(category);
        List<NewsCategory> childCategories = (from p in this.allCategories
                                              where p.ParentId == category.ID
                                              orderby p.Order
                                              select p).ToList<NewsCategory>();
        foreach (NewsCategory p in childCategories)
        {
            node.Nodes.AddRange(this.CreateChildNode(p));
        }
        if (childCategories.Count > 0)
        {
            node.Leaf = false;
        }
        else
        {
            node.Leaf = true;
        }
        nodes.Add(node);
        return nodes;
    }

    [DirectMethod]
    public void DeleteCategory(int id)
    {
        try
        {
            if (this.allCategories.FindAll(p => p.ParentId == id).Count > 0)
            {
                Util.ShowMessage2(Constants.ERROR_DELETE_CATEGORY_HAS_CHILD);
                //ExtNet.Msg.Hide();
            }
            else
            {
                this.repository.Delete(id);
                this.repository.SubmitChanges();
                Util.ShowMessage(Constants.UPDATE_SUCCESS);
                new NOAH.CMS.Cache.NewsCategoryCacheLayer().InvalidateCache();
                this.RegisterReloadTree();
                //ExtNet.Msg.Hide();

            }
        }
        catch (Exception)
        {
        }
        //X.Msg.Hide();
    }

    [DirectMethod]
    public string NodeLoad()
    {
        TreeNodeCollection nodes = new TreeNodeCollection();
        TreeNode rootNode = new TreeNode
        {
            Text = "Menu tin bài",
            NodeID = "-1"
        };
        rootNode.CustomAttributes.Add(new ConfigItem("Name", "Menu tin bài", ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("Url", string.Empty, ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("IsOnHomePage", string.Empty, ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("IsTop", string.Empty, ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("IsBottom", string.Empty, ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("IsLeft", string.Empty, ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("IsRight", string.Empty, ParameterMode.Value));
        rootNode.CustomAttributes.Add(new ConfigItem("Order", string.Empty, ParameterMode.Value));
        rootNode.SingleClickExpand = true;
        rootNode.Expanded = true;
        rootNode.Draggable = false;
        List<NewsCategory> topCategories = (from p in this.allCategories
                                            where p.ParentId == 0
                                            orderby p.Order
                                            select p).ToList<NewsCategory>();
        foreach (NewsCategory category in topCategories)
        {
            rootNode.Nodes.AddRange(this.CreateChildNode(category));
        }
        rootNode.Leaf = topCategories.Count == 0;
        nodes.Add(rootNode);
        return nodes.ToJson();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.repository = new NewsCategoryRepository();
        this.allCategories = this.repository.GetAll();
    }

    private void RegisterReloadTree()
    {
        this.SM.RegisterClientScriptBlock("callBack", "reload();");
    }

    [DirectMethod]
    public void UpdateNewsCategoryRelationship(string currentID, string newParentID, int index)
    {
        try
        {
            NewsCategoryRepository repository = new NewsCategoryRepository();
            NewsCategory currentCategory = repository.Get(int.Parse(currentID));
            if (newParentID == "-1")
            {
                currentCategory.ParentId = 0;
            }
            else
            {
                currentCategory.ParentId = int.Parse(newParentID);
            }
            repository.SubmitChanges();
            Util.ShowMessage(Constants.UPDATE_SUCCESS);
            this.RegisterReloadTree();
        }
        catch (Exception)
        {
        }
    }
}